package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.scity.beens.MetorNewsBeen;
import com.example.scity.beens.MetroBannerBeen;
import com.example.scity.beens.MetroNewsTypeBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CitymetorActivity extends AppCompatActivity {
    Banner banner;
    List<String> list = new ArrayList<>();
    MetroBannerBeen metroBannerBeen;
    LinearLayout intoyijian;
    LinearLayout intoqrcode;
    RadioGroup radioGroup;
    MetroNewsTypeBeen metroNewsTypeBeen;
    MetorNewsBeen metorNewsBeen;
    ListView listView;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    banner.setImages(list);
                    banner.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object o, ImageView imageView) {
                            Glide.with(context).load(o).into(imageView);
                        }
                    });
                    banner.setDelayTime(2000);
                    banner.start();
                    break;
                case 1:
                    LoadRadioGroup();
                    break;
                case 2:
                    LoadMetorListView();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_citymetor);
        banner = findViewById(R.id.citymetoe_banner);
        intoyijian = findViewById(R.id.metro_yijian);
        intoqrcode = findViewById(R.id.metro_qrcode);
        radioGroup = findViewById(R.id.metro_radiogroup);
        listView = findViewById(R.id.metro_newstype_listview);

        LoadBanner();
        IntoYijian();
        LoadNewsType();
        ClickRadioGroup();
        IntoQrcode();
    }

    private void IntoQrcode() {
        intoqrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(CitymetorActivity.this,QRcodeActivity.class));
            }
        });
    }

    private void ClickRadioGroup() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (metroNewsTypeBeen == null) return;
                switch (i) {
                    case R.id.metro_radioButton1:
                        if (metroNewsTypeBeen.data.size() >= 1) {
                            getNewsType(metroNewsTypeBeen.data.get(0).id);
                        }
                        break;
                    case R.id.metro_radioButton2:
                        if (metroNewsTypeBeen.data.size() >= 2) {
                            getNewsType(metroNewsTypeBeen.data.get(1).id);
                        }
                        break;
                    case R.id.metro_radioButton3:
                        if (metroNewsTypeBeen.data.size() >= 3) {
                            getNewsType(metroNewsTypeBeen.data.get(2).id);
                        }
                        break;
                    case R.id.metro_radioButton4:
                        if (metroNewsTypeBeen.data.size() >= 4) {
                            getNewsType(metroNewsTypeBeen.data.get(3).id);
                        }
                        break;
                    case R.id.metro_radioButton5:
                        if (metroNewsTypeBeen.data.size() >= 5) {
                            getNewsType(metroNewsTypeBeen.data.get(4).id);
                        }
                        break;
                }
            }
        });
    }

    private void IntoYijian() {
        intoyijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(CitymetorActivity.this, YjfkActivity.class));
            }
        });
    }

    private void LoadMetorListView() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return metorNewsBeen.rows.size();
            }

            @Override
            public Object getItem(int i) {
                return metorNewsBeen.rows.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                View v = LayoutInflater.from(CitymetorActivity.this).inflate(R.layout.newslistview_item, null);
                ImageView imageView;
                imageView = v.findViewById(R.id.newsimage);
                Glide.with(CitymetorActivity.this).load(Configs.Host + metorNewsBeen.rows.get(i).cover).into(imageView);
                TextView textView1;
                textView1 = v.findViewById(R.id.textTitle);
                textView1.setText(metorNewsBeen.rows.get(i).title);
                TextView textView2;
                textView2 = v.findViewById(R.id.textcontent);
                textView2.setText(Html.fromHtml(metorNewsBeen.rows.get(i).content));
                TextView textView3;
                textView3 = v.findViewById(R.id.textTime);
                textView3.setText(metorNewsBeen.rows.get(i).publishDate);
                TextView textView4;
                textView4 = v.findViewById(R.id.textPingjia);
                textView4.setText("评论数：" + metorNewsBeen.rows.get(i).commentNum + "");
                return v;
            }
        });
    }

    private void LoadRadioGroup() {
        for (int i = 0; i < metroNewsTypeBeen.data.size(); i++) {
            switch (i) {
                case 0:
                    ((RadioButton) findViewById(R.id.metro_radioButton1)).setText(metroNewsTypeBeen.data.get(i).name);
                    break;
                case 1:
                    ((RadioButton) findViewById(R.id.metro_radioButton2)).setText(metroNewsTypeBeen.data.get(i).name);
                    break;
                case 2:
                    ((RadioButton) findViewById(R.id.metro_radioButton3)).setText(metroNewsTypeBeen.data.get(i).name);
                    break;
                case 3:
                    ((RadioButton) findViewById(R.id.metro_radioButton4)).setText(metroNewsTypeBeen.data.get(i).name);
                    break;
                case 4:
                    ((RadioButton) findViewById(R.id.metro_radioButton5)).setText(metroNewsTypeBeen.data.get(i).name);
                    break;
            }
        }
        radioGroup.check(R.id.metro_radioButton1);
    }

    private void getNewsType(String Type) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host + APIInterface.MetroNews + "?type=" + Type).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                metorNewsBeen = new Gson().fromJson(response.body().string(), MetorNewsBeen.class);
                handler.sendEmptyMessage(2);
            }
        });
    }

    private void LoadNewsType() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host + APIInterface.MetroNewsType).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                metroNewsTypeBeen = new Gson().fromJson(response.body().string(), MetroNewsTypeBeen.class);
                handler.sendEmptyMessage(1);
            }
        });
    }

    private void LoadBanner() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.MetroBanner)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                metroBannerBeen = new Gson().fromJson(response.body().string(), MetroBannerBeen.class);
                for (int i = 0; i < metroBannerBeen.rows.size(); i++) {
                    list.add(Configs.Host + metroBannerBeen.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(0);
            }
        });
    }
}